Hi all,
Main question, I have a text file, with some content that closely resembles HTML markup (because it is) however, the way the text is arranged I can extract some information from it. The following is the content of the file (literally). The exact same text.
<div><b>EmployeeName:</b> Luckas Duckins</div> <div><b>CCName:</b> Mike McMice</div> <div><b>CCEmail:</b> MikeMcMice@funinc.com</div> <div><b>ExpirationDate:</b> 7/17/2015</div>
I have a script that was working last Friday, but when I went back today to keep working on it, I got no match, <strike>so I wonder what is it that I was doing last Friday that I did not do today</strike>. Script as follows:
$MyPath = "c:\Path\to\textfile.txt" $regex99 = @' (?ms)<div><b>EmployeeName:<\/b> (.+?)</div> <div><b>CCName:<\/b> (.+?)<\/div> <div><b>CCEmail:<\/b> (.+?)<\/div> <div><b>ExpirationDate:<\/b> (.+?)<\/div> '@ [IO.File]::ReadAllText($MyPath) -match $regex99 if ([IO.File]::ReadAllText($Mypath) -match $regex99) { $EmployeeName = $matches[1] $CCName = $matches[2] $CCEmail = $matches[3] $ExpirtationDate = $matches[4] } "EmpName" $EmployeeName "CC Name" $CCName "CC Email" $CCEmail "EXP Date" $ExpirtationDate #output was #True #EmpName #Luckas Duckins #CC Name #Mike McMice #CC Email #MikeMcMice@funinc.com #EXP Date #7/17/2015
<strike>Right now I just get a big False</strike>. I suspect the issue may be regarding the file itself. I resaved the file (after adding a new line at the end of the file), and the script worked. Then, I removed the new line, and the script works. If I try either of the following regex, each one works, but I am trying to get it on one go.
$regex99 = @' (?ms)<div><b>EmployeeName:<\/b>\s(.+?)<\/div> '@ $regex99 = @' (?ms)<div><b>CCName:<\/b>\s(.+?)<\/div> '@ $regex99 = @' (?ms)<div><b>CCEmail:<\/b>\s(.+?)<\/div> '@
I have used https://mjolinor.wordpress.com/2012/01/05/powershell-multiline-regex-matching/ as a reference, as well as a post I found on Stackoverflow <strike>(cannot find it anymore :( )</strike>
Any help is appreciated.
UPDATE:
Found the post on Stackoverflow that I used as reference. http://stackoverflow.com/questions/15375921/powershell-parse-parts-of-a-text-file-and-save-to-csv
UPDATE 2:
I kept working on the script and I modified the text file, so basically after resaving the file the script worked.
Background about the text file. I get the text content from another script, I save the text on the text file, then I read the file to process it.
Is it possible to save the text to a variable, and keep the text as a here string o I can process it?
- Edited by Mr. Potter III Monday, July 20, 2015 8:20 PM Solved